package com.sugon.springboot.listener;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sugon.springboot.config.GateWayNacosConfig;
import com.sugon.springboot.service.DynamicRouteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executor;

@Component
@Slf4j
public class GateWayNacosListener implements CommandLineRunner {
    @Autowired
    private DynamicRouteService dynamicRouteService;
    @Autowired
    private GateWayNacosConfig gateWayNacosConfig;

    @Autowired
    private ObjectMapper objectMapper;
    @Override
    public void run(String... args) throws Exception {
        this.nacosDynamicRouteListener();
    }
    public void nacosDynamicRouteListener(){
        System.out.printf("1");
        System.out.printf("xiao"+this.gateWayNacosConfig.getDataId()+this.gateWayNacosConfig.getGroup()+this.gateWayNacosConfig.getTimeout());
        System.out.println("dada"+gateWayNacosConfig.getRouteProperties());
        try {
            ConfigService configService = NacosFactory.createConfigService(
                    this.gateWayNacosConfig.getRouteProperties());
            String content = configService.getConfig(this.gateWayNacosConfig.getDataId(),
                    this.gateWayNacosConfig.getGroup(),this.gateWayNacosConfig.getTimeout());
            System.out.printf("content"+content);
            GateWayNacosListener.this.setRoute(content);
            configService.addListener(this.gateWayNacosConfig.getDataId(),
                    this.gateWayNacosConfig.getGroup(), new Listener() {
                        @Override
                        public Executor getExecutor() {
                            return null;
                        }

                        @Override
                        public void receiveConfigInfo(String s) {
                            GateWayNacosListener.this.setRoute(s);
                        }
                    });
        } catch (NacosException e) {
            throw new RuntimeException(e);
        }
    }
    private void setRoute(String info){
        try{

            RouteDefinition[] routeDefinitions = this.objectMapper.readValue(info,RouteDefinition[].class);
            for (RouteDefinition route:routeDefinitions){
                System.out.println("设置路由"+route);
                this.dynamicRouteService.update(route);
            }
        }catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
